{extend name="sitehome@style/base"/}
{block name="resources"}
{/block}
{block name="main"}
<div class="nc-functional-block">
    <button class="layui-btn" onclick="location.href='{:addon_url('Article://sitehome/article/addarticle')}'";>新建文章</button>
</div>
<div class="nc-table-box">
    <table id="article_list" lay-filter="article" class="layui-table"></table>
</div>

<script type="text/html" id="num_info">
	<span>{{d.click}}&nbsp;/&nbsp;{{d.comment_count}}</span>
</script>

<script type="text/html" id="time_info">
<span>{{ nc.time_to_date(d.create_time) }}</span>
</script>

<script type="text/html" id="sort_info">
	<input type="number" value="{{d.sort}}" class="layui-input nc-len-short" onchange="sort({{d.article_id}}, this)"/>
</script>

<script type="text/html" id="operation">
	<a class="default" lay-event="edit">编辑</a>
	<a class="default" lay-event="del">删除</a>
</script>

{/block}
{block name="script"}
<script>
var table = new Table({
	elem: '#article_list',
	filter : "article",
	url : '{:addon_url("article://sitehome/article/articlelist")}',
	cols: [
		[{
			type: 'checkbox',
            unresize : 'true'
		}, {
			field: 'title',
			width: '35%',
			title: '标题',
            unresize : 'true'
		},  {
			field: 'category_name',
			width: '15%',
			title: '分类',
            unresize : 'true'
		}, {
			field: '',
			width: '15%',
			title: '点击量 / 评论量',
			templet: '#num_info',
            unresize : 'true'
		},{
			field: '',
			width: '15%',
			title: '排序',
			templet: '#sort_info',
            unresize : 'true'
		},{
			title: '操作',
			toolbar: '#operation',
			align: 'right',
            unresize : 'true'
		}]
	]
});

//监听工具条
table.tool(function(obj){
	var data = obj.data;
	switch (obj.event) {
          case "edit":
		    window.location.href = nc.url('article://sitehome/article/editarticle', {"article_id":data.article_id});
			break;
          case "del":
		    deleteArticle(data.article_id);
			break;
	}
});

//删除
function deleteArticle(article_id){
    layer.confirm('确定删除吗?', {
        btn: ['确定','取消']
    }, function(){
        $.ajax({
            type:"post",
            async:false,
			url : '{:addon_url("article://sitehome/article/deleteArticle")}',
            data: { 'article_id' : article_id },
            dataType : "JSON",
			success:function(res){
				layer.msg(res.message);
				if(res.code == 0){
					table.reload();
				}
			}
    });
    }, function(){
        layer.close();
    });
}

/**
 * 文章排序
 */
function sort(article_id, event){
	var sort = $(event).val();
	$.ajax({
		type:"post",
        async:false,
        url : '{:addon_url("article://sitehome/article/sortArticle")}',
		data:{'article_id':article_id, 'sort': sort},
		dataType : "JSON",
		success: function (res) {
			layer.msg(res.message);
			if(res.code == 0){
				table.reload();
        	}
		}
	});
}
</script>
{/block}